###  
# A helper function to select specific types of variables from the dataset.
select_data <- function(typevar, codebook) {
  a <- codebook %>%
    filter(type == typevar) %>%
    select(name) %>%
    data.frame()
  return(data[colnames(data)[colnames(data) %in% a$name]])
}
###

# A helper function to select choices from a specific list.
select_listvars <- function(listname, choices) {
  a <- choices %>%
    filter(list == listname) %>%
    select(name,label) %>%
    as.vector()
  return(a)
}
###

###
# Function to set labels for figures
# Inputs are dataset as well as "variable" and "list" in quotation marks
labeler <- function(dataset, var, list, choices) {
  return(set_labels(as.character(dataset[[var]]),
                    labels = (setNames(as.character(select_listvars(list, choices)$name), 
                                       select_listvars(list, choices)$label))) %>% 
           as_label() %>% 
           factor(levels = select_listvars(list, choices)$label))
}
###